package cn.getech.data.development.service.impl;

import cn.getech.data.development.constant.JobTypeEnum;
import cn.getech.data.development.entity.JobDepHistory;
import cn.getech.data.development.entity.JobRunHistory;
import cn.getech.data.development.handler.LogCustomCellWriteHandler;
import cn.getech.data.development.model.vo.LogDownloadData;
import cn.getech.data.development.model.vo.WorkFlowLogVO;
import cn.getech.data.development.model.vo.WorkJobAndDepVO;
import cn.getech.data.development.service.DownloadLogService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DownloadLogServiceImpl implements DownloadLogService {

    @Override
    public void downloadJobLog(HttpServletResponse response, List<WorkJobAndDepVO> workFlowLogList) {

        try {
            //转换要下载的数据
            List<LogDownloadData> targetDownloadDataList =covertDataList(workFlowLogList);

            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), LogDownloadData.class)
                    .registerWriteHandler(new LogCustomCellWriteHandler())
                    .autoCloseStream(Boolean.TRUE).sheet("离线任务运维列表")
                    .doWrite(targetDownloadDataList);
        } catch (Exception e) {
            e.printStackTrace();
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("flag", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            try {
                response.getWriter().println(JSON.toJSONString(map));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }






    /**
     * @Description  获取数据
     * @Author  chengweiping
     * @Date   2020/11/13 14:04
     */
    private List<LogDownloadData> covertDataList( List<WorkJobAndDepVO> dataList) {
        List<LogDownloadData> downloadDataList = new ArrayList<LogDownloadData>();

        if(CollectionUtil.isEmpty(dataList)){
            return downloadDataList;
        }

        downloadDataList=dataList.stream().map(e->{
            LogDownloadData targetDownloadData=new LogDownloadData();
            BeanUtil.copyProperties(e,targetDownloadData);
            String jobTypeStr=JobTypeEnum.getWorkflowNameByCode(e.getJobLogType());
            targetDownloadData.setJobLogTypeStr(jobTypeStr);
            targetDownloadData.setCycleStr(e.getCycleWay());
            targetDownloadData.setRunTypeStr(e.getSheduleWay());


            return targetDownloadData;
        }).collect(Collectors.toList());

        return downloadDataList;
    }







}
